//
//  LXExtension+Task.swift
//  LXMoyaHelper
//
//  Created by yoctech on 2021/8/6.
//

import Foundation
import Moya

public extension Task {
    static func post(params:Any) -> Task {
        if let data = params as? Data {
            return .requestData(data)
        }
        
        guard JSONSerialization.isValidJSONObject(params) else {
            print("请求body入参无法用Json解析: \(params)")
            return .requestPlain
        }
        
        guard let data = try? JSONSerialization.data(withJSONObject: params, options: .prettyPrinted) else {
            print("请求body入参无法用Json解析: \(params)")
            return .requestPlain
        }
        return .requestData(data)
    }
    
    static func get(params:[String:Any]) -> Task {
        return .requestCompositeData(bodyData: Data(), urlParameters: params)
    }
}
